package controllers

import (
	"databaseStudy/models"
	"databaseStudy/models/globalValues"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"log"
)

type UpdateEmpController struct {
	beego.Controller
}
type TransEmpController struct {
	beego.Controller
}

func (c UpdateEmpController) Post() {
	token:=c.GetString("token")
	res:=globalValues.CheckToken(token)
	if !res{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	emp := models.EmployeeInfo{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &emp); err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	log.Println(emp)
	if err:=models.UpdateEmpData(&emp);err!=nil{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	c.Ctx.ResponseWriter.WriteHeader(200)

}

func (c TransEmpController) Post()  {
	token:=c.GetString("token")
	res:=globalValues.CheckToken(token)
	if !res{
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	emp := models.EmployeeInfo{}
	trans:=models.TransferInfo{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &emp); err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &trans); err != nil {
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	log.Println(emp,trans)
	if err:=models.TransEmpData(&emp,&trans);err!=nil{
		log.Println(err)
		c.Ctx.ResponseWriter.WriteHeader(400)
	}
	c.Ctx.ResponseWriter.WriteHeader(200)
}
